import {LocalTokenKey} from './constants';
export default {
    getLocalToken: (): string => {
        let token = ''
        if (window.localStorage) {
            try{
                token = window.localStorage.getItem(LocalTokenKey) || ''
            }catch(e){

            }
        }
        return token
    },
    setLocalToken(token: string) {
        if (window.localStorage) {
            window.localStorage.setItem(LocalTokenKey, token)
        }
    },
    removeLocalToken() {
        if (window.localStorage) {
            window.localStorage.removeItem(LocalTokenKey)
        }
    },
    downloadBinaryFile(binFile: any, fileName: string) {
        // 处理二进制数据并创建 Blob 对象
        const blobObj = new Blob([binFile])
        // 创建一个链接并设置下载属性
        const downloadLink = document.createElement('a')
        let url: any = window.URL || window.webkitURL || window.moxURL // 兼容不同浏览器的 URL 对象
        url = url.createObjectURL(blobObj)
        downloadLink.href = url
        downloadLink.download = fileName // 设置下载的文件名
        // 将链接添加到 DOM 中，模拟点击
        document.body.appendChild(downloadLink)
        downloadLink.click()
        // 移除创建的链接和释放 URL 对象
        document.body.removeChild(downloadLink)
        window.URL.revokeObjectURL(url)
    }
}
